CN.record = "位嘉宾";
var msg=CN;
var guest = {edit_status:"create",uid:site.uid};
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            index = 1,
            td = function(arg){
        	if(!arg)arg="-";
            return "<td>"+arg+"</td>";           
            },
            td_title = function(arg,title){
            	if(!arg)arg="-";
                return "<td title='"+title+"'>"+arg+"</td>";           
                },
            setButton = function(data){
            	return "<a class='btn btn-info btn-mini' data-id='"+data.egid+"' status='0' view='off'>移除</a>";
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="" type="button"  key="'+kk.egid+'" status="0" view="off">'+kk.cname+'</button>';
             	tdValue =td(index)+ 
            	td(u_btn)+
            	td(kk.ename)+ 
            	td(kk.company)+ 
            	td(kk.mobile)+
            	td(kk.email)+
            	td_title(substring(kk.desc,30),kk.desc);
               	rowclass = element%2;//行样式
               	index++;
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
    mpage.setext("?eid="+getUrlParam("eid"));
	mpage.init();
}); 

function init(){
	init_dialog();
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		guest.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		guest.name = $("#name").val();
		guest.desc = $("#desc").val();
		guest.url = $("#url").val();
		guest.level = $("#level").val();
		guest.uid = site.uid;
		guest.requesturl = site.web+"my/"+site.title+"/"+guest.edit_status;//包含create/update操作
		guest.eid = getUrlParam("eid");
		guest.evid = $("#evid").val();
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
				
			}else{
				$('#myModal').modal('hide');
			}
			
		}
		GG(guest,complete);
	});
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	
}

clear_dialog = function(){
	$("#form1 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
}

open_dialog = function(data){
	clear_dialog();
	switch(guest.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");
			$.each(data, function(index, value) {
				if(index !="eid" && index !="w" && index !="h"){
					$("#"+index).val(value);
				}
			});
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			if(data.data.length==1){
				guest.edit_status = "update";
				open_dialog (data.data[0]);
			}
		}else{
			alert("ERROR guest 404");
		}
	}
	var url = site.web+"my/"+site.title+"/get";
	var json = {url:url,evid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+"my/"+site.title+"/del";
	var json = {url:url,evid:id,uid:site.uid};
	G(json,cb);
}

